package com.java.carousel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.java.carousel.mapper.CarouselMapper;
import com.java.carousel.service.CarouselService;
import com.java.pojo.Carousel;
import com.java.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CarouselServiceImpl implements CarouselService {
    @Autowired
    private CarouselMapper carouselMapper;

    /**
     *轮播图查询数据，并将前6个数据发送
     * @return 图片信息
     */
    @Cacheable(value = "list.carousel",key = "#root.methodName",cacheManager = "cacheManagerDay")
    @Override
    public R list() {
        //绑定查询数据
        QueryWrapper<Carousel> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("priority");
        //查询结果
        List<Carousel> carouselList = carouselMapper.selectList(queryWrapper);
        //选取优先级最高的前6个
        List<Carousel> collect = carouselList.stream().limit(6).collect(Collectors.toList());
        log.info("图片信息为：",collect);
        return R.ok(collect);
    }
}
